package xyz.caijiwang.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import java.text.DecimalFormat;


public class CheckMoneyUtil {


    private String local;
    private Double weight;
    private Double kilometre;

    public CheckMoneyUtil(String local, double weight, double kilometre) {
        this.local = local;
        this.kilometre = kilometre;
        this.weight = weight;
    }


    private Double getoilmoney() {
        try {
            RestTemplate restTemplate = new RestTemplate();
            String str = restTemplate.getForObject("http://oil.ylapi.cn/todayoil/info.u?uid=11547&appkey=ff1fa67a89a7e8080c4948a6726a16a4&prov=" + local, String.class);
            JSONObject strj = new JSONObject();
            strj = JSON.parseObject(str);
            JSONObject strm = new JSONObject();
            strm = JSON.parseObject(strj.get("data").toString());
            return Double.parseDouble((String) strm.get("p0"));
        } catch (HttpClientErrorException e) {
            System.out.println("http客户端请求出错了！");
            return null;
        }
    }

    private Double getweightandroadmoney() {
        if (kilometre < 250.0) {
            return 200.0;
        }
        if (weight <= 10.0) {
            return 0.08 * weight * kilometre;
        } else if (weight <= 40.0 && weight > 10.0) {
            return (((-1 / 750) * weight) + (7 / 75)) * kilometre;
        } else if (weight > 40.0) {
            return 0.04 * weight * kilometre;
        }
        return null;
    }

    public Double getfinalmoney() {
//        this.local = local;
//        this.weight = weight;
//        this.kilometre = kilometre;
        Double getoilmoney = getoilmoney();
        getoilmoney = getoilmoney * 2 / 5 * kilometre;
        Double getweightandroadmoney = getweightandroadmoney();
        DecimalFormat df = new DecimalFormat("######0.00");
        return Double.parseDouble(df.format(getoilmoney + getweightandroadmoney + (getoilmoney + getweightandroadmoney) * 0.4));
    }

}
